package cn.dapeng.tree.multi;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * 对数器， 验证多叉树查找
 */
public class MultiTreeTest {
    public static String SAMPLE = """
            我爱北京天安门天安门上太阳升伟大领袖毛主席带领我们向前进红军不怕远征难万水天山只等闲我爱我们的祖国我很一切不
            爱好和平的人小日本狗杂种美国佬不得好死西方文化必败中国必胜白日依山尽黄河入海流欲穷千里目更上一层楼春眠不觉晓处处闻啼鸟夜来风雨声花落知多少
            今天星很多啊希望今年粮食大丰收大家都开心过个好年远看寒山石径斜白云山深处有人家不识庐山真面目只缘身在此山中
            """;


    public static void main(String[] args) {

        int testTimes = 20000;
        int maxLength = 8;
        Set<String> treeContent = new HashSet<>();
        Set<String> words = new HashSet<>();
        for(int i = 0; i < testTimes; i ++) {
            String ek = getWords(maxLength);
            List<String> list = treeContent.stream().parallel().filter(t -> ek.startsWith(t) || t.startsWith(ek)).toList();

            if(list.isEmpty()) {
                treeContent.add(ek);
            }

            words.add(getWords(maxLength));
        }

        treeContent.forEach(System.out::println);

        for(String content : treeContent) {
            MultiTreeCreator.createMultiTree(content);
        }

        //MultiTreeCreator.formatToString(MultiTreeCreator.FormatType.Line);
        words.add(treeContent.toArray(new String[0])[0]);
        for(String word : words) {
            boolean f1 = MultiTreeCreator.contains(word);
            boolean f2 = treeContent.contains(word);

            if(f1 != f2) {
                System.out.println("Oops" + word);
            } else if(f1) {
                System.out.println("Over" + word);
            }

        }

    }

    public static String getWords(int size) {
        SAMPLE = SAMPLE.replaceAll("\\s+", "");
        int length = SAMPLE.length();
        StringBuilder sb = new StringBuilder();

        int maxLength = (int) (Math.random() * size);

        while (maxLength < 2) {
            maxLength = (int) (Math.random() * size);
        }

        for (int i = 0; i < maxLength; i++) {
            sb.append(SAMPLE.charAt((int) (Math.random() * length)));
        }
        return sb.toString();
    }

}
